package com.itheima.entity;


public class Result {

    /**
     * {
     *     "code":1,
     *     "msg":"查询图书成功",
     *     "data":{
     *       "id": 1,
     *       "type": "计算机",
     *       "name": "Java从入门到精通2",
     *       "description": "Java从入门到精通2，只要努力，都能成功！"
     *     }
     * }
     */

    //返回状态码，1成功 0失败
    private Integer code;
    //返回的消息内容   "新增成功"  "新增失败，书名已存在"
    private String msg;
    //附加数据   如果是查询操作，当前得到的集合或者对象
    private Object data;

    public static Result success(String msg, Object data){
        Result result = new Result();
        result.setCode(1);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    // 特定的成功消息，需要通过这种方式来完成
    public static Result success(Integer code, String msg, Object data){
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public static Result error(String msg, Object data){
        Result result = new Result();
        result.setCode(0);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    // 特定的错误消息，需要通过这种方式来完成
    public static Result error(Integer code, String msg, Object data){
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
